package com.tjetc.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.tjetc.constant.CommonConstant;
import com.tjetc.dao.RoomDao;
import com.tjetc.dao.impl.RoomDaoImpl;
import com.tjetc.exception.MessageException;
import com.tjetc.pojo.dto.RoomDTO;
import com.tjetc.pojo.entity.Room;
import com.tjetc.service.RoomService;
import com.tjetc.utils.DruidUtils;

import java.util.List;

/**
 * @Author: itda
 * @Date: 2024/5/10 18:09
 * @Description: RoomServiceImpl
 */
public class RoomServiceImpl implements RoomService {
    private RoomDao roomDao = new RoomDaoImpl();

    @Override
    public void save(RoomDTO roomDTO) {
        try {
            DruidUtils.start();
            Room room = new Room();
            BeanUtil.copyProperties(roomDTO, room);
            roomDao.save(room);
            DruidUtils.commit();
            System.out.println(CommonConstant.ADD_SUCCESS);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            DruidUtils.rollback();
        }
    }

    @Override
    public void delete(Long id) {
        try {
            DruidUtils.start();
            roomDao.deleteById(id);
            DruidUtils.commit();
            System.out.println(CommonConstant.DELETE_SUCCESS);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            DruidUtils.rollback();
        }
    }

    @Override
    public void update(RoomDTO roomDTO) {
        try {
            DruidUtils.start();
            Room room = new Room();
            BeanUtil.copyProperties(roomDTO, room);
            roomDao.update(room);
            DruidUtils.commit();
            System.out.println(CommonConstant.UPDATE_SUCCESS);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            DruidUtils.rollback();
        }
    }

    @Override
    public void list() {
        try {
            List<Room> list = roomDao.list();
            if (list == null || list.size() == 0) {
                throw new MessageException(CommonConstant.NO_FIND_DATA);
            }
            list.forEach(System.out::println);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    @Override
    public void getByStatus(int status) {
        try {
            List<Room> list = roomDao.getByStatus(status);
            if (list == null || list.size() == 0) {
                throw new MessageException(CommonConstant.NO_FIND_DATA);
            }
            list.forEach(System.out::println);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    @Override
    public List<Room> listByStatus(int i) {
        try {
            return roomDao.getByStatus(i);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return null;
    }
}
